package com.atguigu.eduservice.controller;


import com.alibaba.excel.util.StringUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.entity.vo.CourseTable;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduCourseService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-08-06
 */

@Api(description = "课程管理")
@RestController
@RequestMapping("/eduservice/edu-course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    //课程列表
    @ApiOperation(value = "条件分页课程列表")
    @PostMapping("{current}/{limit}")
    public R getCourseList(@PathVariable long current, @PathVariable long limit,
                           @RequestBody(required = false) CourseQuery courseQuery) {

        List<CourseTable> courseTables = eduCourseService.getPage(current, limit, courseQuery);
        int total = eduCourseService.getTotal(courseQuery);
        return R.ok().data("total",total).data("rows",courseTables);
    }

    //添加课程信息
    @ApiOperation("添加课程信息")
    @PostMapping("addCourseInfo")
    public R addCourseInfo(
            @ApiParam(name = "course", value = "课程信息", required = true)
            @RequestBody CourseInfoVo courseInfoVo ) {

        String cid = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",cid);
    }

    //根据课程id查询课程信息
    @ApiOperation("查询课程信息")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);

        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //根据课程id查询课程信息
    @ApiOperation("修改课程信息")
    @PutMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程ID查询课程确认信息o
    @ApiOperation("课程发布时的确认信息")
    @GetMapping("getPublishCourseInf/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId) {

        CoursePublishVo coursePublishVo = eduCourseService.publishCourse(courseId);
        return R.ok().data("coursePublishVo",coursePublishVo);
    }

    //发布课程
    @ApiOperation("课程最终发布,修改课程状态")
    @PutMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setStatus("Normal");
        eduCourse.setId(courseId);
        //根据课程Id将eduCourse中的其他属性值赋给原来的值。
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    //删除课程
    @ApiOperation("删除课程")
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        //删除多个视频

        eduCourseService.removeCourse(courseId);
        return R.ok().data("result","删除成功");
    }





}

